﻿

"use strict"
define(['admin/app', "service/admin/distributionService"], function (app) {

    app.controller("distributionController", function ($scope, $state, $stateParams, utils, baseService, distributionService) {
        $scope.psdisabled = false;
        $scope.sel_sku_info = [];
        $scope.list = {};
        $scope.category = {};
        $scope.goods_service_items = []
        $scope.shop_service_items = [];
        $scope.query = {
            page: 1, size: 20, sort: 1, keyword: '', sell_out: 0, sale_status: 0, audit_status: 0
        };
        $scope.merchantquery = {
            page: 1, size: 20, mobile: '', start_time: '', end_time: '', nick: '', open_wx: '', merchant_no: '', parent_id: 0, certify: 0, auth_status: '', disabled: '', is_oem: '', is_platform: '', is_supplier: ''
        };
        $scope.out_rate = 0;
        $scope.editConfig = {
            autoHeightMode: true,
            items: ['source', 'preview', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'multiimage', 'link']
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            //查询以及获取全部数据
            search: function () {
                distributionService.gets($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            searchmerchant: function () {
                distributionService.searchmerchant($scope.merchantquery).success(function (response) {
                    $scope.list = response.data;
                });
            },
            //分销商户
            distributionmerchant: function (id) {
                var modal = utils.confirm({ msg: "是否要分销该商户", ok: "确定", cancel: "取消"});
                modal.result.then(function () {
                    distributionService.distributionmerchant({ id: id }).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            utils.remove($scope.model, model);
                    })
                })
            },
            //获取供应商基本信息
            detail: function () {
                distributionService.detail({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                });
            },

            //获取分类
            get_category: function () {
                distributionService.get_categorys().success(function (response) {
                    $scope.category = response.data;
                });
            },
            //获取规格
            get_sku_info: function (id) {
                distributionService.get_sku_info({ merchant_id: 6 }).then(function (response) {
                    $scope.sel_sku_infos = response.data;
                });
            },
          
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
            searchmerchant_page: function (page) {
                $scope.merchantquery.page = page;
                methods.searchmerchant('page', $scope.merchantquery.page);
            },
            savemerchant: function (params) {
                var model = $scope.model;
                distributionService.savemerchant({ model: model }).success(function (response) {
                    utils.notify("操作成功", response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.goBack();
                    }
                });
            },
            save: function (params) {
                if (params.categoryid == 'undefined') {
                    alert("请选择产品分类");
                }
                else {
                    distributionService.save({ categoryid: params.categoryid, id: params.id }).success(function (response) {
                        utils.notify(response.data.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000) {
                            methods.search();
                        }
                    })
                }
            },
            //获取对应分销商下商品
            get_products: function () {
                distributionService.get_products().success(function (response) {
                    if (response.result_code == 1000) {
                        $scope.list = response.data;
                    }
                });
            },
            sold_out: function (id) {
                distributionService.sold_out(id).success(function () {
                    utils.notify(response.data.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.search();
                    }
                });
            },
            goBack: function () {
                history.back();
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.product.detail') || $state.includes('app.product.audit_detail')) {
            methods.edit();
        }
        else if ($state.includes('app.distribution.merchantlist'))
        {
            methods.searchmerchant();
        }
        else if ($state.includes("app.distribution.detail"))
        {
            methods.detail();
        }
        else if ($state.includes('app.distribution.product_remove'))
        {
            methods.get_products();
        }
        else {
            methods.get_category();
            methods.search();
        }
    });
    
    
});